package ipsite.controller;

public class Paginate {
	public static int page(String str)
	{
		int p = 1;
		if(str != null)
		{
			str = str.replaceAll("[^0-9]", "");
			if(!str.equals(""))
			{
				p = Integer.parseInt(str);
			}
		}
		return p;
	}
	public static String paginate(int curpage,int total,int viewLink,String path)
	{
		String str = "<ul class='paginate'>";
		
		if(curpage < (viewLink-viewLink/2))
		{
			int end = (viewLink < total)?viewLink:total;
			for(int i=1;i<=end;i++)
			{
				if(i != curpage)
				{
					str += "<li><a href=\""+path+"p="+i+"\">"+i+"</a></li>";
				} else {
					str += "<li class='p_acive'><span>"+i+"</span></li>";
				}
			}
		} else {
			int end = ((curpage+viewLink/2) < total)?curpage+viewLink/2:total;
			for(int i=curpage-viewLink/2;i<=end;i++)
			{
				if(i != curpage)
				{
					str += "<li><a href=\"?p="+i+"\">"+i+"</a></li>";
				} else {
					str += "<li class='p_acive'><span>"+i+"</span></li>";
				}
			}
		}
		str += "</ul>";
		return str;
	}
}
